package com.qj.daibei.controller;

import com.qj.daibei.pojo.DataFiling;
import com.qj.daibei.pojo.User;
import com.qj.daibei.service.DataFilingService;
import com.qj.daibei.service.UserService;
import com.qj.daibei.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * author wml
 * Created on 2021/9/29 15:59
 */
@Controller
public class UserCustomController {

    @Autowired
    private UserService userService;

    @Autowired
    private DataFilingService dataFilingService;

    // wml
    @RequestMapping("/borrowing")
    public String borrowing(HttpServletRequest request) {
        // 通过session获取登录的用户信息
        HttpSession session = request.getSession(false);
        Integer userId = ((User) session.getAttribute("loginUser")).getUserId();
        // 根据用户编号查询用户信息
        User user = this.userService.queryForUserById(userId);
        // 根据用户编号查询资质信息
        DataFiling dataFiling = this.dataFilingService.queryForRecordByUserId(userId);
        if (user.getFillIn().equals("0")) { // 检查用户表是否填写
            return "myInformation";
        } else if (dataFiling.getFillIn().equals("0")) { // 检查资质表是否填写
            return "dataFiling";
        } else if (dataFiling.getAssessmentTypeA() == 1 || dataFiling.getAssessmentTypeA() == 2 || (dataFiling.getAssessmentTypeA() == 4 && (dataFiling.getAssessmentTypeB() == 1 || dataFiling.getAssessmentTypeB() == 2))) { // 检查资质是否审核通过
            return "tjptsh";
        } else {
            return "rzjg";
        }
    }

    @RequestMapping("/updateUser")
    public ResponseEntity<String> updateUser(User user, @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession(false);
        User tempUser = (User) session.getAttribute("loginUser");
        user.setUserId(tempUser.getUserId());
        user.setFillIn("1");
        if (!multipartFile.isEmpty()) {
            String fileName = FileUpload.doSingleFileUpload(multipartFile, "avatar");
            user.setAvatar(fileName);
        }

        System.out.println(user);
        int result = this.userService.updateByUserId(user);
        System.out.println(result);
        if (result == 1) {
            User loginUser = this.userService.queryForUserById(tempUser.getUserId());
            session.setAttribute("loginUser", loginUser);
            return new ResponseEntity<String>("ok", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("error", HttpStatus.OK);
        }
    }

    @RequestMapping("/queryUser")
    @ResponseBody
    public User queryUser(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        return this.userService.queryForUserById(user.getUserId());
    }

    @RequestMapping(value = "getPinMoneyByUserId", method = RequestMethod.GET)
    @ResponseBody
    public Float getPinMoneyByUserId(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        return this.userService.queryForUserById(user.getUserId()).getPinMoney();
    }
}
